Discord.py mass dm bot - python-3.x

I've been trying to find out a way to make a discord bot dm everybody inside of my server. I have already found a question similar to mine and tried the answer but it didn't work. My current code looks like this
if message.content.upper().startswith('.MSG'):
if "345897570268086277" in [role.id for role in message.author.roles]:
member = discord.Member
args = message.content.split(" ")
if member == "#everyone":
for server_member in server.members:
await client.send_message(server_member, "%s" % (" ".join(args[1:])))

I would use the commands extension for this, not on_message
# import stuff we'll be using
from discord.ext import commands
from discord.utils import get
# Bot objects listen for commands on the channels they can "see" and react to them by
# calling the function with the same name
bot = commands.Bot(command_prefix='.')
# Here we register the below function with the bot Bot
# We also specify that we want to pass information about the message containing the command
# This is how we identify the author
#bot.command(pass_context=True)
# The command is named MSG
# The `, *, payload` means take everything after the command and put it in one big string
async def MSG(ctx, *, payload):
# get will return the role if the author has it. Otherwise, it will return None
if get(ctx.message.author.roles, id="345897570268086277"):
for member in ctx.message.server.members:
await bot.send_message(member, payload)
bot.run('token')
How sure are you that "345897570268086277" is the role id? It might make more sense to search for it by name.

Related

Why does it show me a sum of 0?

I just wrote a discord bot and used a function to show me the members of each guild the bot is in. That worked all the time but now i wanted to use a other server so i copied my files and dragged them on the other server. But for some reason now it says the sum of all the member = 0. Can someone help me?
Thx in advance ^^
async def member():
while True:
for guild in my_guild_id: # list of guild id's
guild = bot.get_guild(guild)
member = sum(discord.member and not member.bot for member in guild.members)
for channel in members_channel_id: # list of channel id's
check = str(guild.get_channel(channel)).encode('ascii', 'ignore')
if check != none: # none = b'None' cause there were some bugs
channel = bot.get_channel(channel)
await channel.edit(name=f"👥Servermember: {Member}")
await asyncio.sleep(20)
Your function has the same name as a variable in the function, you should be using a different name for it.
Also, none takes a capitalized N: None.
Finally, you're using the variable Member when editing the channel, but this variable isn't defined, python is case-sensitive.
I don't think you just "copied and dragged" your files, and that couldn't work on another server...

Giving a role to another user with DiscordPy

I am trying to use a command to give a specific role using DiscordPy, but every search I use brings me to the same answers, that don't help: 1, 2, 3.
Clearly, I'm missing a fundamental part, but i have no idea what. The documentation, covers that there is a add_roles command, but that doesn't give any explanation of how to use it for another user. In fact, trying await add_roles("Team Captain") gives the error NameError: name 'add_roles' is not defined.
What am I missing here? Why does add_roles not exist when it's documented, and how do I use it against a different user.
This is (some of) what I have at the moment, but obviously, doesn't work:
import discord, discord.utils, random, os, re, json
from discord.ext import commands
from discord.utils import get
from dotenv import load_dotenv
client = discord.Client()
load_dotenv()
key = os.getenv('DISCORD_KEY')
#client.event
async def on_message(message):
if message.author == client.user:
return
print('Message from {0.author}: {0.content}'.format(message))
#Want these commands in the right channel
if str(message.channel).lower().startswith("bot"):
if message.content.lower().startswith("$addcaptain"):
if len(message.mentions) == 0:
await message.channel.send("Needs a user to give Team Captain role.")
return
else:
await add_roles("Team Captain") #Doesn't work, and I assume would be against the user who sent the message, not the mentioned one
client.run(key)
key = os.getenv('DISCORD_KEY')
add_roles is a method that belongs to the Member object. This means that you'll need to get the target member, in your case message.mentions[0] as message.mentions returns a list of Members, and then stick .add_roles(..) on the end of it.
Additionally, when adding a role, it accepts Role objects, not just a name or ID. This means you'll need to fetch the role first, which can be done in a multitude of ways, but the one I'll use is utils.get() (other methods such as Guild.get_role() are also available)
This brings us to your code:
#client.event
async def on_message(message):
# code
if str(message.channel).lower().startswith("bot"):
if message.content.lower().startswith("$addcaptain"):
if len(message.mentions) == 0:
await message.channel.send("Needs a user to give Team Captain role.")
return
else:
role = discord.utils.get(message.guild.roles, name="Team Captain")
await message.mentions[0].add_roles(role)
References:
utils.get()
Message.mentions - message.mentions[0] gets the first element of the list, which is the discord.Member that was mentioned.
Member.add_roles()
Guild.get_role() - If you want to use this, you'll do:
role = message.guild.get_role(112233445566778899)
where 112233445566778899 is the Team Captain's role ID.

If...else statements not working as expected

I'm actually trying to create a simple Discord bot using discord.py. The problem comes here, when I try to execute this code:
import discord
from discord.ext import commands
myid = 3586xxxxxxxxxx
cliente = commands.Bot(command_prefix="!!")
#cliente.command()
async def clear(ctx, amount):
if myid == ctx.author.id:
print("Entering If statement") # This is printed
await ctx.channel.purge(limit=int(amount)) # This is not executed
else:
await ctx.channel.send("You don't have enough permissions.") # This is executed
The output makes no sense, when I run "!!clear 2" on my server, the program enters the If and prints "Entering the If statement", It doesn't remove anything, and then the bot sends the message inside the else.
I'm so confused right now :s
I still don't know why It didn't work, but hey!, I found a great alternative that consists on using usernames. I'll leave an example:
import discord
from discord.ext import commands
myuname = "User#1234"
cliente = commands.Bot(command_prefix="!!")
#cliente.command()
async def clear(ctx, amount):
if myuname == str(ctx.author): # For some reason using str() is neccessary or It won't work properly.
print("Entering If statement") # This is printed
await ctx.channel.purge(limit=int(amount)) # This is not executed
else:
await ctx.channel.send("You don't have enough permissions")
And that's It, hope It helped someone :)

using Subprocess to avoid long-running task from disconnecting discord.py bot?

I created a bot for my Discord server, that goes to the Reddit API for a given subreddit, and posts the Top 10 results for the Day in the Discord chat, based on the subreddit(s) that you input. It disregards self posts, and really only posts pictures and GIFs. The Discord message command would look something like this: =get funny awww news programming, posting the results for each subreddit as it gets them from the Reddit API (PRAW). THIS WORKS WITH NO PROBLEM. I know that the bot's ability to hit the API and post to discord works.
I added another command =getshuffled which puts all of the results from the subreddits in a large list, and then shuffles them before posting. This works really well with a request of up to ~50 subreddits.
This is what I need help with:
Because it can be such a large list of results, 1000+ results from 100+ subreddits, the bot is crashing on really big requests. Based on what help I got from my question yesterday, I understand what is going wrong. The bot is starting, it is talking to my Discord server, and when I pass it a long request, it stops talking to the server for too long while the Reddit API call is done, and it the Discord connection fails.
So, what I think I need to do, is have a subprocess for the code that goes to the Reddit API and pulls the results, (which I think will let the discord connection stay running), and then pass those results BACK to the bot when it is finished....
Or... this is something that Asyncio can handle on its own...
I'm having a hard time with the subprocess call, as I knew I would.
Basically, I either need help with this subprocess trickery, or need to know if I'm being an idiot and Asyncio can handle all of this for me. I think this is just one of those "I don't know what I don't know" instances.
So to recap: The bot worked fine with smaller amounts of subreddits being shuffled. It goes through the args sent (which are subreddits), grabbing info for each post, and then shuffling before posting the links to discord. The problem is when it is a larger set of subreddits of ~ 50+. In order to get it to work with the larger amount, I need to have the Reddit call NOT block the main discord connection, and that's why I'm trying to make a subprocess.
Python version is 3.6 and Discord.py version is 0.16.12
This bot is hosted and running on PythonAnywhere
Code:
from redditBot_auth import reddit
import discord
import asyncio
from discord.ext.commands import Bot
#from discord.ext import commands
import platform
import subprocess
import ast
client = Bot(description="Pulls posts from Reddit", command_prefix="=", pm_help = False)
#client.event
async def on_ready():
return await client.change_presence(game=discord.Game(name='Getting The Dank Memes'))
def is_number(s):
try:
int(s)
return True
except:
pass
def show_title(s):
try:
if s == 'TITLES':
return True
except:
pass
async def main_loop(*args, shuffled=False):
print(type(args))
q=10
#This takes a integer value argument from the input string.
#It sets the number variable,
#Then deletes the number from the arguments list.
title = False
for item in args:
if is_number(item):
q = item
q = int(q)
if q > 15:
q=15
args = [x for x in args if not is_number(x)]
if show_title(item):
title = True
args = [x for x in args if not show_title(x)]
number_of_posts = q * len(args)
results=[]
TESTING = False #If this is turned to True, the subreddit of each post will be posted. Will use defined list of results
if shuffled == False: #If they don't want it shuffled
for item in args:
#get subreddit results
#post links into Discord as it gets them
#The code for this works
else: #if they do want it shuffled
output = subprocess.run(["python3.6", "get_reddit.py", "*args"])
results = ast.literal_eval(output.decode("ascii"))
# ^^ this is me trying to get the results back from the other process.
. This is my get_reddit.py file:
#THIS CODE WORKS, JUST NEED TO CALL THE FUNCTION AND RETURN RESULTS
#TO THE MAIN_LOOP FUNCTION
from redditBot_auth import reddit
import random
def is_number(s):
try:
int(s)
return True
except:
pass
def show_title(s):
try:
if s == 'TITLES':
return True
except:
pass
async def get_results(*args, shuffled=False):
q=10
#This takes a integer value argument from the input string.
#It sets the number variable,
#Then deletes the number from the arguments list.
title = False
for item in args:
if is_number(item):
q = item
q = int(q)
if q > 15:
q=15
args = [x for x in args if not is_number(x)]
if show_title(item):
title = True
args = [x for x in args if not show_title(x)]
results=[]
TESTING = False #If this is turned to True, the subreddit of each post will be posted. Will use defined list of results.
NoGrabResults = False
#This pulls the data and creates a list of links for the bot to post
if NoGrabResults == False:
for item in args:
try:
#get the posts
#put them in results list
except Exception as e:
#handle error
pass
try:
#print('____SHUFFLED___')
random.shuffle(results)
random.shuffle(results)
random.shuffle(results)
except:
#error stuff
print(results)
#I should be able to read that print statement for the results,
#and then use that in the main bot function to post the results.
.
#client.command()
async def get(*args, brief="say '=get' followed by a list of subreddits", description="To get the 10 Top posts from a subreddit, say '=get' followed by a list of subreddits:\n'=get funny news pubg'\n would get the top 10 posts for today for each subreddit and post to the chat."):
#sr = '+'.join(args)
await main_loop(*args)
#THIS POSTS THE POSTS RANDOMLY
#client.command()
async def getshuffled(*args, brief="say '=getshuffled' followed by a list of subreddits", description="Does the same thing as =get, but grabs ALL of the posts and shuffles them, before posting."):
await main_loop(*args, shuffled=True)
client.run('my ID')
UPDATE: Following advice, I had the command passed through a ThreadPoolExecutor as shown:
async def main(*args, shuffled):
if shuffled==True:
with concurrent.futures.ThreadPoolExecutor() as pool:
results = await asyncio.AbstractEventLoop().run_in_executor(
executor=pool, func=await main_loop(*args, shuffled=True))
print('custom thread pool', results)
but this still results in errors when the script tries to talk to Discord:
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<Client._run_event() running at /home/GageBrk/.local/lib/python3.6/site-packages/discord/client.py:307> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f28acd8db28>()]>>
Event loop is closed
Destination must be Channel, PrivateChannel, User, or Object. Received NoneType
Destination must be Channel, PrivateChannel, User, or Object. Received NoneType
Destination must be Channel, PrivateChannel, User, or Object. Received NoneType
...
It is sending the results correctly, but discord is still losing connection.
praw relies on the requests library, which is synchronous meaning that the code is blocking. This can cause your bot to freeze if the blocking code takes too long to execute.
To get around this, a separate thread can be created that handles the blocking code. Below is an example of this. Note how blocking_function will use time.sleep to block for 10 minutes (600 seconds). This should be more than enough to freeze and eventually crash the bot. However, since the function is in it's own thread using run_in_executor, the bot continues to operate as normal.
New versions
import time
import asyncio
from discord.ext import commands
from concurrent.futures import ThreadPoolExecutor
def blocking_function():
print('entering blocking function')
time.sleep(600)
print('sleep has been completed')
return 'Pong'
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('client ready')
#client.command()
async def ping(ctx):
loop = asyncio.get_event_loop()
block_return = await loop.run_in_executor(ThreadPoolExecutor(), blocking_function)
await ctx.send(block_return)
client.run('token')
Older async version
import time
import asyncio
from discord.ext import commands
from concurrent.futures import ThreadPoolExecutor
def blocking_function():
print('entering blocking function')
time.sleep(600)
print('sleep has been completed')
return 'Pong'
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('client ready')
#client.command()
async def ping():
loop = asyncio.get_event_loop()
block_return = await loop.run_in_executor(ThreadPoolExecutor(), blocking_function)
await client.say(block_return)
client.run('token')

How to get ID of a mentioned user (Discord.py)

#bot.command()
async def id(ctx, a:str): #a = #user
how would I get the ID of a user mentioned in the command, and output it as:
await ctx.send(id)
Use a converter to get the User object:
#bot.command(name="id")
async def id_(ctx, user: discord.User):
await ctx.send(user.id)
Or to get the id of the author:
#bot.command(name="id")
async def id_(ctx):
await ctx.send(ctx.author.id)
Just realized that when you #someone and store it to the variable "a", it contains the user ID in the form of '<#userid>'. So a bit of clean up can get me the user ID
Here's the code:
#bot.command()
async def id(ctx, a:str):
a = a.replace("<","")
a = a.replace(">","")
a = a.replace("#","")
await ctx.send(a)
Since my command consists of "rev id #someone", the #someone gets stored in 'a' as the string '<#userid>' instead of '#someone'.
If you want to handle a mention within your function, you can get the mention from the context instead of passing the mention as a string argument.
#bot.command()
async def id(ctx):
# Loop through the list of mentioned users and print the id of each.
print(*(user_mentioned.id for user_mentioned in ctx.message.mentions), sep='\n')
ctx.message.mentions will return:
A list of Member that were mentioned. If the message is in a private
message then the list will be of User instead.
When you loop through ctx.message.mentions, each item is a mentioned member with attributes such as id, name, discriminator. Here's another example of looping through the mentioned list to handle each member who was mentioned:
for user_mentioned in ctx.message.mentions:
# Now we can use the .id attribute.
print(f"{user_mentioned}'s ID is {user_mentioned.id}")
It's up to you whether you want to require the argument a as shown in the question above. If you do need this, note that the string will sometimes include an exclamation in the mention depending on whether it is:
for a User or command was posted from mobile app: <#1234567890>
for a Nickname or command was posted from desktop app: <#!1234567890>
Which is why I prefer to get the id from a member/user attribute.

Resources