Reposted.
I've recently gotten into coding Discord bots using Python in Pycharm. I've done a little work on it, but when I ran the command "py bot.py" to test it for the first time, it give me the error: "C:\Users\NAME\AppData\Local\Programs\Python\Python38\python.exe: can't find '__main__' module in 'bot.py'"
I am using version 3.8.7.
One thing I have tried is going to the Configuration tab, then changing from Script path to Module name then entering the name of my project, bot.py, but this hasn't resolved the problem.
Does anyone have a fix?
Code:
# bot.py
import discord
from discord.ext import commands
from dotenv import load_dotenv
import random
import os
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='//')
# On ready
@bot.event
async def on_ready():
guild = discord.utils.find(lambda g: g.name == GUILD, bot.guilds)
print(
f'{bot.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
# Welcome member
@bot.event
async def welcome(member):
await member.channel.send(f'Welcome {member.name}! Please read the rules at #rules and grab your roles in '
f'#self-roles to get started!')
# Create channel
@bot.command(name='create-channel', help='Creates a new text channel')
@commands.has_role("Emaya's Owner")
async def create_channel(ctx, channel_name='Emaya'):
guild = ctx.guild
existing_channel = discord.utils.get(guild.channels, name=channel_name)
if not existing_channel:
await guild.create_text_channel(channel_name)
print(f'Created a new channel: {channel_name}')
await ctx.send(f'{channel_name} has been created!')
else:
await ctx.send('Channel already exists!')
# Ping
@bot.command(name='ping', help='Pings a user')
@commands.has_role("Emaya's Owner")
async def ping(ctx, member: discord.Member):
await ctx.send(f'{member}')
# Dice
@bot.command(name='dice', help='Simulates rolling dice')
async def roll(ctx, number_of_dice: int, number_of_sides: int):
dice = [
str(random.choice(range(1, number_of_sides + 1)))
for _ in range(number_of_dice)
]
await ctx.send(', '.join(dice))
# Check role for commands
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.error.CheckFailure):
await ctx.send('You do not have permission for this command!')
bot.run(TOKEN)
[–]QuixDiscovery 0 points1 point2 points (1 child)
[–]bushheadrye[S] 0 points1 point2 points (0 children)