检查某人是否在语音通道中出现不一致

时间:2020-11-06 19:20:30

标签: python discord discord.py

我在Discord机器人中发出了一个命令,该命令在发送命令G!tutturu时播放音频文件。当您处于语音通道中时,它可以正常工作,但是当您不在而不是向用户发送不在通道中时,会出现错误。我知道错误的含义,但无法找到解决方法。

import discord
import os
from discord.ext.commands import Bot
import datetime
from discord.ext import commands
import asyncio
import praw
import audioread
import time

TOKEN = "____"
bot = commands.Bot(command_prefix="G!")
client = discord.Client()
split_1 = "I'm"
split_2 = "i'm"
split_3 = "Im"
split_4 = "im"
rest = None
reddit = praw.Reddit(client_id='', client_secret='',
                     username='', password='', user_agent='test')

subreddit = reddit.subreddit('GameDeals')
new_GameDeals = subreddit.new(limit=50)


@bot.command(name='tutturu')
async def tutturu(ctx):
    path_mp3 = "Tutturuu.mp3"
    path_ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe"
    voice_channel = ctx.author.voice.channel
    if voice_channel is not None:
        vc = await voice_channel.connect()
        vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
        with audioread.audio_open(path_mp3) as f:
            await asyncio.sleep(f.duration)
        await vc.disconnect()
    else:
        await ctx.channel.send(str(ctx.author.name) + "is not in a channel.")
    await ctx.message.delete()


@bot.command(name='GameDeals')
async def GameDeals(ctx):
    for post in new_GameDeals:
        if '[STEAM]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Steam]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Epic Games]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Uplay]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Humble Bundle]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[Epic]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)
        elif '[UPLAY]' in post.title and ('free' in post.title or 'Free' in post.title or '100%' in post.title):
            await ctx.message.channel.send(post.title)


@bot.command(name='Help')
async def Help(ctx):
    await ctx.message.channel.send('1) G!tutturu speelt tutturu af')
    await ctx.message.channel.send('2) G!GameDeals toont je de gratis game deals van het moment')


@bot.event
async def on_message(message):
    if 'Creeper' in message.content or 'creeper' in message.content and not message.author.bot:
        await message.channel.send('OW MAN')
    if 'ah fuck' in message.content or 'Ah fuck' in message.content and not message.author.bot:
        await message.channel.send("I can't believe you've done this")
    if "Who didn't flush the toilet when they took a shat" in message.content and not message.author.bot:
        await message.channel.send('DISGUUUSTAAAN!!!!!')
        await message.channel.send(file=discord.File('disgustang.png'))
    if '❤' in message.content and not message.author.bot:
        await message.channel.send('❤')
    if message.content == 'koekoek' or message.content == 'Koekoek' and not message.author.bot:
        await message.channel.send(datetime.datetime.now())
    if "I'm" in message.content and not message.author.bot:
        rest = message.content.partition(split_1)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if "i'm" in message.content and not message.author.bot:
        rest = message.content.partition(split_2)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if "Im" in message.content and not message.author.bot:
        rest = message.content.partition(split_3)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if "im" in message.content and not message.author.bot:
        rest = message.content.partition(split_4)[2]
        await message.channel.send('Hello' + rest + ", I'm dad")
    if 'fire' in message.content or 'Fire' in message.content or 'lit' in message.content or 'Lit' in message.content and not message.author.bot:
        await message.channel.send('?')
    if 'geil' in message.content or 'Geil' in message.content or 'lekker' in message.content or 'Lekker' in message.content and not message.author.bot:
        await message.channel.send('?')
    await bot.process_commands(message)


@bot.event
async def on_voice_state_update(member: discord.Member, before, after):
    vc_before = before.channel
    vc_after = after.channel
    path_mp3 = "Tutturuu.mp3"
    path_ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe"
    for role in member.roles:
        if role.name == "Gerbinbot 3000":
            return
    if vc_before == vc_after:
        return
    if vc_before is None:
        channel = member.voice.channel
        vc = await channel.connect()
        await asyncio.sleep(.5)
        vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
        with audioread.audio_open(path_mp3) as f:
            await asyncio.sleep(f.duration)
        await vc.disconnect()
    elif vc_after is None:
        return
    else:
        channel = member.voice.channel
        vc = await channel.connect()
        await asyncio.sleep(.5)
        vc.play(discord.FFmpegPCMAudio(executable=path_ffmpeg, source=path_mp3))
        with audioread.audio_open(path_mp3) as f:
            await asyncio.sleep(f.duration)
        await vc.disconnect()
bot.run(TOKEN)

错误:

Ignoring exception in command tutturu:
Traceback (most recent call last):
  File "C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot_3000\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/yorbe/OneDrive/Documenten/Folders/Gerbinbot_3000/main.py", line 30, in tutturu
    voice_channel = ctx.author.voice.channel
AttributeError: 'NoneType' object has no attribute 'channel'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot_3000\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot_3000\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\yorbe\OneDrive\Documenten\Folders\Gerbinbot_3000\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'

1 个答案:

答案 0 :(得分:0)

问题在这里:

voice_channel = ctx.author.voice.channel
    if voice_channel is not None:

Member.voice在该成员不在语音通道中时返回None,因此您应该先检查一下。

voice_state = ctx.author.voice
if voice is not None:
    ....