Пример #1
0
def test_is_village_win(roles, expect):
    game = Game()
    for role in roles:
        player = Player(1)
        player.set_role(role)
        game.players.append(player)
    assert game.is_village_win() is expect
Пример #2
0
    async def d100(self, ctx, limit=-1):

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        r = _random(100)
        msg = ""
        if limit == -1:
            msg = f'1D100の結果は{r}です'
        else:
            if r <= limit and r <= 5:
                msg = f'1D100の結果は{r}でクリティカル'
            elif r <= limit:
                msg = f'1D100の結果は{r}で成功'
            elif limit < r and 96 <= r:
                msg = f'1D100の結果は{r}でファンブル'
            else:
                msg = f'1D100の結果は{r}で失敗'
        await ctx.send(f'{ctx.author.name}さんの{msg}')

        if self.bot.games[f'{ctx.guild.id}'].status == Status.PLAYING:
            voice_client = ctx.message.guild.voice_client
            ffmpeg_audio_source = discord.FFmpegPCMAudio("dice.mp3")
            voice_client.play(ffmpeg_audio_source)
        if self.bot.games[f'{ctx.guild.id}'].players.is_joined(ctx.author.id):
            self.bot.games[f'{ctx.guild.id}'].logs.append(
                f'{ctx.author.name}さんの{msg} :: <{Game.get_time()}>')
            self.bot.games[f'{ctx.guild.id}'].players.get(
                ctx.author.id).logs.append(f'{msg} :: <{Game.get_time()}>')
Пример #3
0
def main() -> None:
    logger: Logger = setup_logger(__name__)
    logger.info("START")

    # 接頭辞を「;」に設定
    bot: WerewolfBot = WerewolfBot(command_prefix=";")
    bot.game = Game()
    logger.debug(bot)

    # 環境変数DISCORD_BOT_TOKENからBOTのトークンを取得
    token = os.environ["DISCORD_BOT_TOKEN"]

    # コマンド「;ping」を追加
    # コマンドの内容は ping()を参照
    bot.add_command(ping)

    extension_list = [
        "cogs.players_cog",
        "cogs.game_status_cog",
    ]
    for extension in extension_list:
        bot.load_extension(name=extension)

    # bot実行
    bot.run(token)
Пример #4
0
    async def change_date(self, ctx):
        """日付変更処理"""
        if not self.bot.game.is_set_target():
            await self.bot.game.channel.send('指定が行われましたが、未指定の方がいます。')
            return

        guild = self.bot.game.channel.guild

        self.bot.game.execute()

        executed = guild.get_member(self.bot.game.executed.id)
        text = f'投票の結果 {executed.display_name} さんが処刑されました'
        await self.bot.game.channel.send(text)

        if self.bot.game.is_village_win():
            text = 'ゲームが終了しました。人狼が全滅したため村人陣営の勝利です!'
            await self.bot.game.channel.send(text)
            self.bot.game = Game()
            return

        self.bot.game.raid()

        if self.bot.game.raided is not None:
            raided = guild.get_member(self.bot.game.raided.id)
            text = f'{raided.display_name} さんが無残な姿で発見されました'
            await self.bot.game.channel.send(text)

        if self.bot.game.is_werewolf_win():
            text = 'ゲームが終了しました。村人陣営の数が人狼陣営の数以下になったため人狼陣営の勝利です!'
            await self.bot.game.channel.send(text)
            self.bot.game = Game()
            return

        self.bot.game.fortune()

        if self.bot.game.fortuned is not None:
            text = f'占い結果は {self.bot.game.fortuned} です。'
            await guild.get_member(self.bot.game.players.fortuneteller.id
                                   ).send(text)

        for p in self.bot.game.players.alives:
            p.clear_vote().clear_raid().clear_fortune()

        self.bot.game.days += 1
Пример #5
0
    async def secret(self, ctx):

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        r = _random(100)
        msg = f'シークレットダイスの結果は{r}です'
        user_id = ctx.message.author.id
        user = self.bot.get_user(user_id)
        await user.send(msg)
        if self.bot.games[f'{ctx.guild.id}'].players.is_joined(ctx.author.id):
            self.bot.games[f'{ctx.guild.id}'].players.get(
                ctx.author.id).logs.append(f'{msg} :: <{Game.get_time()}>')
Пример #6
0
    async def close(self, ctx):
        """セッション終了"""
        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.NOTHING:
            await ctx.send('セッションが立ってません')
            return
        if self.bot.games[f'{ctx.guild.id}'].status == Status.WAITING:
            self.bot.games[f'{ctx.guild.id}'].status = Status.NOTHING
            await ctx.send('セッションをキャンセルします')
            self.bot.games[f'{ctx.guild.id}'] = Game()
            return
        self.bot.games[f'{ctx.guild.id}'].status = Status.NOTHING
        await ctx.send('セッションを終了します')

        for p in self.bot.games[f'{ctx.guild.id}'].players:
            filename = f'{p.id}log.txt'
            with open(filename, 'w') as f:
                for log in p.logs:
                    f.write(log + '\n')
            await ctx.send(file=discord.File(filename))
            os.remove(filename)
            await ctx.send(f'{p.name}さんのログを出力しました')
        with open('gamelog.txt', 'w') as f:

            for log in self.bot.games[f'{ctx.guild.id}'].logs:
                f.write(log + "\n")

        await ctx.send(file=discord.File('gamelog.txt'))
        os.remove('gamelog.txt')

        if ctx.guild.voice_client is not None:
            client = ctx.guild.voice_client
            await client.disconnect()
        self.bot.game = Game()
Пример #7
0
    async def leave(self, ctx):
        """セッションへの参加をやめる(脱退)"""

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.NOTHING:
            return await ctx.send("セッションが立っていません")
        elif self.bot.games[f'{ctx.guild.id}'].status == Status.PLAYING:
            return await ctx.send("セッションが既に開始されているため退出出来ません")
        mem = ctx.author
        for p in self.bot.games[f'{ctx.guild.id}'].players:
            if mem.id == p.id:
                self.bot.games[f'{ctx.guild.id}'].players.remove(mem.id)
                self.bot.games[f'{ctx.guild.id}'].logs.append(
                    f'{mem.name}さんがセッションから退出しました :: <{Game.get_time()}>')
                return await ctx.send(f'{p.name}さんがセッションから退出しました')
Пример #8
0
    async def mylog(self, ctx):
        """自分のログファイルを出力"""

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.NOTHING:
            return await ctx.send("セッションが立っていません")
        elif self.bot.games[f'{ctx.guild.id}'].status == Status.WAITING:
            return await ctx.send("セッションが開始されていません")
        p = self.bot.games[f'{ctx.guild.id}'].players.get(ctx.author.id)
        filename = f'{ctx.author.name}log.txt'
        with open(filename, 'w') as f:
            for log in p.logs:
                f.write(log + '\n')
        await ctx.send(file=discord.File(filename))
        os.remove(filename)
        await ctx.send(f'{ctx.author.name}さんのログを出力しました')
Пример #9
0
    async def sessionlog(self, ctx):
        """ゲーム全体のログファイルを出力"""

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.NOTHING:
            return await ctx.send("セッションが立っていません")
        elif self.games[f'{ctx.guild.id}'].bot.status == Status.WAITING:
            return await ctx.send("セッションが開始されていません")
        with open('gamelog.txt', 'w') as f:

            for log in self.bot.games[f'{ctx.guild.id}'].logs:
                f.write(log + "\n")

        await ctx.send(file=discord.File('gamelog.txt'))
        os.remove('gamelog.txt')

        await ctx.send("ゲームログを出力しました")
Пример #10
0
    async def dice(self, ctx, d_count=3, d_max=6):

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        msg, num = diceroll(d_count, d_max)
        await ctx.send(f'{ctx.author.name}さんの{d_count}D{d_max}\n{msg}')

        if self.bot.games[f'{ctx.guild.id}'].status == Status.PLAYING:
            voice_client = ctx.message.guild.voice_client
            ffmpeg_audio_source = discord.FFmpegPCMAudio("dice.mp3")
            voice_client.play(ffmpeg_audio_source)
        if self.bot.games[f'{ctx.guild.id}'].players.is_joined(ctx.author.id):
            self.bot.games[f'{ctx.guild.id}'].logs.append(
                f'{ctx.author.name}さんの{d_count}D{d_max} -> {num} :: <{Game.get_time()}>'
            )
            self.bot.games[f'{ctx.guild.id}'].players.get(
                ctx.author.id).logs.append(
                    f'{d_count}D{d_max} -> {num} :: <{Game.get_time()}>')
Пример #11
0
    async def start(self, ctx):
        """セッション開始"""

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.NOTHING:
            await ctx.send('セッションが立ってません')
            return
        if self.bot.games[f'{ctx.guild.id}'].status == Status.PLAYING:
            await ctx.send('セッション中です')
            return

        self.bot.games[f'{ctx.guild.id}'].status = Status.PLAYING

        await ctx.send('セッション開始しました')
        if ctx.author.voice is not None:
            vc = ctx.author.voice.channel
            await vc.connect()
Пример #12
0
    async def create(self, ctx):
        """セッションを立てる"""

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.PLAYING:
            return await ctx.send('セッション中です')

        if self.bot.games[f'{ctx.guild.id}'].status == Status.WAITING:
            return await ctx.send('セッション準備中')

        self.bot.games[f'{ctx.guild.id}'].status = Status.WAITING
        self.bot.games[f'{ctx.guild.id}'].channel = ctx.channel
        await ctx.send('セッションの準備を開始します')
        mem = Player(ctx.author.id, ctx.author.name, True)
        self.bot.games[f'{ctx.guild.id}'].players.append(mem)
        self.bot.games[f'{ctx.guild.id}'].logs.append(
            f'{ctx.author.name}さんがセッションを立ち上げました ::  <{Game.get_time()}>')
Пример #13
0
    async def join(self, ctx):
        """セッションに参加する"""

        if f'{ctx.guild.id}' not in self.bot.games:
            self.bot.games[f'{ctx.guild.id}'] = Game()

        if self.bot.games[f'{ctx.guild.id}'].status == Status.NOTHING:
            return await ctx.send('セッションが立っていません')

        if self.bot.games[f'{ctx.guild.id}'].status == Status.PLAYING:
            return await ctx.send('セッションが進行中です')

        player = ctx.author
        for p in self.bot.games[f'{ctx.guild.id}'].players:
            if p.id == player.id:
                return await ctx.send('セッション参加済みです')
        mem = Player(player.id, player.name)
        self.bot.games[f'{ctx.guild.id}'].players.append(mem)
        self.bot.games[f'{ctx.guild.id}'].logs.append(
            f'{player.name}さんがセッションに参加しました :: <{Game.get_time()}>')
        self.bot.games[f'{ctx.guild.id}'].players.get(
            player.id).logs.append(f'セッションに参加しました :: <{Game.get_time()}>')
        await ctx.send(f'{player.mention}さんが参加しました')
Пример #14
0
import os
import traceback
from discord.ext import commands
from cogs.utils.game import Game
from cogs.utils.errors import PermissionNotFound, NotGuildChannel, NotDMChannel

bot = commands.Bot(command_prefix='/')
bot.game = Game()

extensions = [
    'cogs.status',
    'cogs.players',
    'cogs.vote',
]
for extension in extensions:
    bot.load_extension(extension)


@bot.event
async def on_command_error(ctx, error):
    """エラーハンドリング"""

    if isinstance(error, commands.CheckFailure):
        return

    if isinstance(error, PermissionNotFound):
        await ctx.send('コマンドを実行する権限がありません')
        return

    if isinstance(error, NotGuildChannel):
        await ctx.send('サーバー内でのみ実行できるコマンドです')
Пример #15
0
import pytest
from cogs.utils.game import Game
from cogs.utils.player import Player

player1 = Player(1)
player2 = Player(2)
player3 = Player(3)
player4 = Player(4)
player5 = Player(5)
game = Game()
game.players.append(player1)
game.players.append(player2)
game.players.append(player3)
game.players.append(player4)
game.players.append(player5)


def test_is_set_target():
    player1.set_vote(player2)
    player2.set_vote(player3)
    player3.set_vote(player4)
    player4.set_vote(player5)
    assert game.is_set_target() is False

    player5.set_vote(player1)
    player1.set_role('狼')
    assert game.is_set_target() is False

    player1.set_raid(player2)
    player5.set_role('占')
    assert game.is_set_target() is False