我只是在我的discord機器人中添加了一些額外的代碼來播放youtube音樂,但我去運行機器人,它像正常一樣啟動并作業,但當我去測驗新的命令時,沒有任何作業,沒有錯誤或任何東西,然后我嘗試基本的命令,如/hello,以前作業正常,也沒有任何作用。我是以行政管理的角色來做機器人的。 我仔細檢查了我沒有拼錯命令,但即使我拼錯了,on_command_error函式也應該呼叫。另外,我在/play命令的開頭做了一個列印除錯陳述句,但連這個陳述句都沒有被呼叫,而且我也沒有改變任何重要的東西。有沒有人有什么想法?
from discord.ext import commands
from dotenv import load_dotenv
from lxml import html
import youtube_dl
import requests
import random
import discord
import requests
import shutil,os
# 載入.env檔案。
load_dotenv()
PREFIX = "/"/span>
bot = commands.Bot(command_prefix=PREFIX)
# EVENTS#
@bot.event
async def on_ready() 。
await bot.get_channel(888736019590053898).send(f "We back online! 這都要感謝*sploosh* :D")
@bot.event[/span].
async def on_command_error(ctx, error) 。
if isinstance(error, commands.CommandNotFound)。
回復 = ["Err這是一個命令嗎?", "你能打字嗎?", "是的...這不是一個命令伙計。", "對不起忘記你不會拼寫"]
await ctx.send(random.choice(replies))
@bot.event[/span
async def on_message(message)。
if str(message.channel) == "images-videos" and message.content != " :
await message.channel.purge(limit=1)
# COMMANDS#
@bot.command()
async def hello(ctx)。
# 獲取一個隨機的事實。
url = 'http://randomfactgenerator.net/': http://randomfactgenerator.net/
page = requests.get(url)
tree = html.fromstring(page.content)
hr = str(tree.xpath('/html/body/div[4]/div[2]/text()'/span>)
await ctx.reply("**Hello Bozo!
" "*Random Fact : *" hr[:-9] "】")
@bot.command()。
async def randomNum(ctx, start: int = None, end:int = None)。)
if(start == None or end == None)。
await ctx.reply("*Check your arguments!
``/randomNum START_NUMBER END_NUMBER``")
else:
randNum = random.randint(start, end)
await ctx.reply(f "*{randNum}*"/span>)
@bot.command()。
@commands.is_owner()。
async def kick(ctx, member:discord. Member = None, *, reason="You smell bozo.")。)
if(member == None):
await ctx.reply("*Check your arguments!
```/kick @MEMBER REASON(optional)```")
elif ctx.author.id in (member.id, bot.user.id)。
await ctx.reply("*你不能踢自己/我,你這個傻子。*")
else:
await member.kick(reason=理由)
@bot.command()
@commands.is_owner()/span>
async def ban(ctx, member:discord. Member = None, *, reason="再見! :D.")。)
if(member == None):
await ctx.reply("*Check your arguments!
```/kick @MEMBER REASON(optional)```")
elif ctx.author.id in (member.id, bot.user.id)。
await ctx.reply("*你不能禁止你自己/我,你這個傻瓜。*")
else:
等待 member.ban(reason=reason)
@bot.command()
@commands.is_owner()。
async def close_bot(ctx)。
回復 = ["Well bye!", "猜我現在要走了嗎?", "請讓我留下..."]
await ctx.send(random.choice(replies))
await bot.close()
# YOUTUBE音頻播放器。
@bot.command()
async def play(ctx, url : str) 。
print("working")
if(url == None):
await ctx.reply("*檢查你的引數!*")
```/play VIDEO_URL```")
else:
song = os.path.isfile(" songs/song.mp3")
try: # 檢查歌曲是否存在,如果存在則將其洗掉。
if(song):
os.remove(" songs/song.mp3")
except PermissionError:
await ctx.reply("*等待當前歌曲結束,或使用'stop' 命令*")
回傳。
voiceChannel = discord.utils.get(ctx.guild.voice_channels, name="休息室")
await voiceChannel.connect()
語音 = discord.utils.get(bot.voice_clients, guild = ctx.guild)
ytdl_opts = { # 你需要傳入的一些選項 'format'。'bestaudio/best'。
'postprocessors':[{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'.
}],
}
with youtube_dl.YoutubeDL(ytdl_opts) as ydl:
ydl.download(url) # Download the vid[/span].
for file in os.listdir("./") 。# Change the name and move to songs folder.
if file.endswith(" .mp3"):
os.rename(file, "song.mp3")
shutil.move("song.mp3", " songs")
voice.play(discord.FFmpegPCMAudio(source="song/song.mp3") #播放歌曲。
@bot.command()
async def leave(ctx)。
語音 = discord.utils.get(bot.voice_clients, guild = ctx.guild)
if(voice.is_connected())。
語音.斷開連接()
else:
等待 ctx.reply("*如果我沒有連接,如何離開一個語音通道?*")
@bot.command()
async def pause(ctx)。
語音 = discord.utils.get(bot.voice_clients, guild = ctx.guild)
if(voice.is_playing())。
語音.暫停()
else:
等待 ctx.reply("*Can't pause buddy, nothings playing...*"/span>)
@bot.command()
async def resume(ctx)。
語音 = discord.utils.get(bot.voice_clients, guild = ctx.guild)
if(voice.is_paused() )。
語音.恢復()
else:
等待 ctx.reply("*不能恢復好友,音頻已經在播放*")
@bot.command()
async def stop(ctx)。
語音 = discord.utils.get(bot.voice_clients, guild = ctx.guild)
語音.停止()
if __name__ == "__main__"/span>:
bot.run(os.getenv("BOT_TOKEN"/span>)
uj5u.com熱心網友回復:
你需要在你的自定義on_message事件里面有bot.process_commands(message),
@bot.event。
async def on_message(message)。
if str(message.channel) == "images-videos" and message.content != " :
await message.delete()
await bot.process_commands(message)
我還把message.channel.purge(limit=1)改為message.delete(),因為它是洗掉訊息的正確方法
關于為什么需要這樣做的更多資訊,請參閱faq
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/329890.html
標籤:
