我目前只是在弄乱乱序编程的窍门,所以我可以制作一个实际的游戏,但是遇到一个问题,当我尝试使python查找文件时,出现了我不理解的Unicode错误。 这是代码:(错误附加为图像):
import discord
import asyncio
from discord.ext import commands
from PIL import Image
bot = discord.Client()
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == "Big red button":
await message.channel.send("Oh no i died")
quit()
elif message.content == "Cinnamon roll":
await message.channel.send("Here you go")
await channel.send(file=discord.File('C:\Users\User\Desktop\Python\Cinnamon_Roll.png'))
client = MyClient()
client.run('TOKEN')
答案 0 :(得分:0)
我没有看到您的错误图片,但是您的路径中包含无效的转义序列。 用以下内容替换第26行:
await channel.send(file=discord.File('C:\\Users\\User\Desktop\Python\Cinnamon_Roll.png'))
答案 1 :(得分:0)
如果这很简单,则无需使用类。请记住,任何人都可以像这样阻止机器人,您可以进行额外检查
if message.content == "Big red button" and message.author.id == 'OWNER_ID_HERE':
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
@bot.event
async def on_ready():
print("Logged in as")
print(bot.user.name)
print("------")
@bot.event
async def on_message(message):
# don't respond to ourselves
if message.author == bot.user:
return
if message.content == "Big red button":
await message.channel.send("Oh no i died")
await bot.logout()
elif message.content == "Cinnamon roll":
await message.channel.send("Here you go")
await message.channel.send(file=discord.File('C:\Users\User\Desktop\Python\Cinnamon_Roll.png'))
bot.run('TOKEN_HERE')
答案 2 :(得分:0)
您也可以执行此操作,而不是用另一个斜杠转义斜杠。
await channel.send(file=discord.File(r'C:\Users\User\Desktop\Python\Cinnamon_Roll.png'))
保留r
符号会使它变原始。
python不接受\
的原因是因为斜杠是特殊的并且用于unicode字符